<?php

namespace App\Http\Controllers;

use App\Test;
use ElemeOpenApi\Api\UgcService;
use ElemeOpenApi\Config\Config;
use Illuminate\Http\Request;

class CommentsController extends Controller
{
    /** 获取指定订单的评论
     * @param $order_id 订单id
     * @return mixed
     */
    public function getOrderRateByOrderId(Request $request)
    {
        $requestData = $request->only([
            'order_id','type','unique_id'
        ]);
        switch($requestData['type']){
            case "Elm":
                $config = new Config(env('PLATFORM_KEY'),env('PLATFORM_SECRET'), true);
                $token = Cache::get('token_'.$requestData['unique_id'],function(){                     throw_error('token获取失败');                 }); 
                $service = new UgcService(json_decode($token),$config);
                $date = $service->get_order_rate_by_order_id($requestData['order_id']);
                    $data[] = [
                        'product_id' => $date->id,
                        'product_shopId' => $date->shopId,
                        'product_rating' => $date->rating,
                        'product_rateContent' => $date->rateContent,
                        'product_ratedAt' => $date->ratedAt,
                        'product_itemRates' => $date->itemRates,
                        'product_orderRateReply' => $date->orderRateReply,
                        'product_rateTag' => $date->rateTag,
                    ];
                return $data;
                break;
            case "Mt":

                break;
            case "Tdd":
                break;
        }
    }


    /** 获取指定订单的评论
     * @param $order_ids 订单id
     * @return mixed
     */
    public function getOrderRatesByOrderIds(Request $request)
    {
        $requestData = $request->only([
            'order_ids','type','unique_id'
        ]);
        switch($requestData['type']){
            case "Elm":
                $config = new Config(env('PLATFORM_KEY'),env('PLATFORM_SECRET'), true);
                $token = Cache::get('token_'.$requestData['unique_id'],function(){                     throw_error('token获取失败');                 }); 
                $service = new UgcService(json_decode($token),$config);
                $dates = $service->get_order_rates_by_order_ids(json_decode($requestData['order_ids']));
                foreach($dates as  $date) {
                    $data[] = [
                        'product_id' => $date->id,
                        'product_shopId' => $date->shopId,
                        'product_rating' => $date->rating,
                        'product_rateContent' => $date->rateContent,
                        'product_ratedAt' => $date->ratedAt,
                        'product_itemRates' => $date->itemRates,
                        'product_orderRateReply' => $date->orderRateReply,
                        'product_rateTag' => $date->rateTag,
                    ];
                }
                return $data;
                break;
            case "Mt":

                break;
            case "Tdd":
                break;
        }
    }

    /** 获取未回复的评论  没测试
     * @param $order_ids 订单id
     * @return mixed
     */
    public function getUnreplyOrderRatesByOrderIds(Request $request)
    {
        $requestData = $request->only([
            'order_ids','type','unique_id'
        ]);
        switch($requestData['type']){
            case "Elm":
                $config = new Config(env('PLATFORM_KEY'),env('PLATFORM_SECRET'), true);
                $token = Cache::get('token_'.$requestData['unique_id'],function(){                     throw_error('token获取失败');                 }); 
                $service = new UgcService(json_decode($token),$config);
                $dates = $service->get_unreply_order_rates_by_order_ids($requestData['order_ids']);
                foreach($dates as  $date) {
                    $data[] = [
                        'product_id' => $date->id,
                        'product_shopId' => $date->shopId,
                        'product_rating' => $date->rating,
                        'product_rateContent' => $date->rateContent,
                        'product_ratedAt' => $date->ratedAt,
                        'product_itemRates' => $date->itemRates,
                        'product_orderRateReply' => $date->orderRateReply,
                        'product_rateTag' => $date->rateTag,
                    ];
                }
                return $data;
                break;
            case "Mt":

                break;
            case "Tdd":
                break;
        }
    }

    /** 获取指定店铺的评论      Operation timed out after 10000 milliseconds with 0 bytes received
     * @param $shop_id  餐厅id
     * @param $start_time   开始时间,只能查询最近90天的数据
     * @param $end_time   结束时间
     * @param $offset 页面偏移量
     * @param $page_size 页面大小
     * @return mixed
     */
    public function getOrderRatesByShopId(Request $request)
    {
        $requestData = $request->only([
            'shop_id','start_time','end_time','offset','page_size','type','unique_id'
        ]);
        switch($requestData['type']){
            case "Elm":
                $config = new Config(env('PLATFORM_KEY'),env('PLATFORM_SECRET'), true);
                $token = Cache::get('token_'.$requestData['unique_id'],function(){                     throw_error('token获取失败');                 }); 
                $service = new UgcService(json_decode($token),$config);
                $date = $service->get_order_rates_by_shop_id($requestData['shop_id'],$requestData['start_time'],$requestData['end_time'],$requestData['offset'],$requestData['page_size']);
                    $data[] = [
                        'product_id' => $date->id,
                        'product_shopId' => $date->shopId,
                        'product_rating' => $date->rating,
                        'product_rateContent' => $date->rateContent,
                        'product_ratedAt' => $date->ratedAt,
                        'product_itemRates' => $date->itemRates,
                        'product_orderRateReply' => $date->orderRateReply,
                        'product_rateTag' => $date->rateTag,
                    ];

                return $data;
                break;
            case "Mt":

                break;
            case "Tdd":
                break;
        }
    }

    /** 获取指定店铺的评论      duo
     * @param $shop_ids 店铺id
     * @param $start_time   开始时间,只能查询最近90天的数据
     * @param $end_time   结束时间
     * @param $offset 页面偏移量
     * @param $page_size 页面大小
     * @return mixed
     */
    public function get_order_rates_by_shop_ids(Request $request)
    {
        $requestData = $request->only([
            'shop_ids','start_time','end_time','offset','page_size','type','unique_id'
        ]);
        switch($requestData['type']){
            case "Elm":
                $config = new Config(env('PLATFORM_KEY'),env('PLATFORM_SECRET'), true);
                $token = Cache::get('token_'.$requestData['unique_id'],function(){                     throw_error('token获取失败');                 }); 
                $service = new UgcService(json_decode($token),$config);
                $dates = $service->get_order_rates_by_shop_ids($requestData['shop_ids'],$requestData['start_time'],$requestData['end_time'],$requestData['offset'],$requestData['page_size']);
                foreach($dates as  $date) {
                    $data[] = [
                        'product_id' => $date->id,
                        'product_shopId' => $date->shopId,
                        'product_rating' => $date->rating,
                        'product_rateContent' => $date->rateContent,
                        'product_ratedAt' => $date->ratedAt,
                        'product_itemRates' => $date->itemRates,
                        'product_orderRateReply' => $date->orderRateReply,
                        'product_rateTag' => $date->rateTag,
                    ];
                }
                return $data;
                break;
            case "Mt":

                break;
            case "Tdd":
                break;
        }
    }

    /** 获取未回复的评论
     * @param $shop_ids 店铺id
     * @param $start_time   开始时间,只能查询最近90天的数据
     * @param $end_time   结束时间
     * @param $offset 页面偏移量
     * @param $page_size 页面大小
     * @return mixed
     */
    public function getUnreplyOrderRatesByShopIds(Request $request)
    {
        $requestData = $request->only([
            'shop_ids','start_time','end_time','offset','page_size','type','unique_id'
        ]);
        switch($requestData['type']){
            case "Elm":
                $config = new Config(env('PLATFORM_KEY'),env('PLATFORM_SECRET'), true);
                $token = Cache::get('token_'.$requestData['unique_id'],function(){                     throw_error('token获取失败');                 }); 
                $service = new UgcService(json_decode($token),$config);
                $dates = $service->get_unreply_order_rates_by_shop_ids($requestData['shop_ids'],$requestData['start_time'],$requestData['end_time'],$requestData['offset'],$requestData['page_size']);
                dd($dates);
                foreach($dates as  $date) {
                    $data[] = [
                        'product_id' => $date->id,
                        'product_shopId' => $date->shopId,
                        'product_rating' => $date->rating,
                        'product_rateContent' => $date->rateContent,
                        'product_ratedAt' => $date->ratedAt,
                        'product_itemRates' => $date->itemRates,
                        'product_orderRateReply' => $date->orderRateReply,
                        'product_rateTag' => $date->rateTag,
                    ];
                }
                return $data;
                break;
            case "Mt":

                break;
            case "Tdd":
                break;
        }
    }

    /** 获取店铺的满意度评价信息      Empty reply from server
     * @param $shop_id  餐厅id
     * @param $score 满意度,取值范围为1~5，1为最不满意，5为非常满意
     * @param $start_time   开始时间,只能查询最近90天的数据
     * @param $end_time   结束时间
     * @param $offset 页面偏移量
     * @param $page_size 页面大小
     * @return mixed
     */
    public function getOrderRatesByShopAndRating(Request $request)
    {
        $requestData = $request->only([
            'shop_id','score','start_time','end_time','offset','page_size','type','unique_id'
        ]);
        switch($requestData['type']){
            case "Elm":
                $config = new Config(env('PLATFORM_KEY'),env('PLATFORM_SECRET'), true);
                $token = Cache::get('token_'.$requestData['unique_id'],function(){                     throw_error('token获取失败');                 }); 
                $service = new UgcService(json_decode($token),$config);
                $dates = $service->get_order_rates_by_shop_and_rating($requestData['shop_id'],$requestData['score'],$requestData['start_time'],$requestData['end_time'],$requestData['offset'],$requestData['page_size']);
                foreach($dates as  $date) {
                    $data[] = [
                        'product_id' => $date->id,
                        'product_shopId' => $date->shopId,
                        'product_rating' => $date->rating,
                        'product_rateContent' => $date->rateContent,
                        'product_ratedAt' => $date->ratedAt,
                        'product_itemRates' => $date->itemRates,
                        'product_orderRateReply' => $date->orderRateReply,
                        'product_rateTag' => $date->rateTag,
                    ];
                }
                return $data;
                break;
            case "Mt":

                break;
            case "Tdd":
                break;
        }
    }

    /** 获取单个商品的评论
     * @param $item_id  商品id
     * @param $start_time   开始时间,只能查询最近90天的数据
     * @param $end_time   结束时间
     * @param $offset 页面偏移量
     * @return mixed
     */
    public function getItemRatesByItemId(Request $request)
    {
        $requestData = $request->only([
            'item_id','start_time','end_time','offset','type','unique_id'
        ]);
        switch($requestData['type']){
            case "Elm":
                $config = new Config(env('PLATFORM_KEY'),env('PLATFORM_SECRET'), true);
                $token = Cache::get('token_'.$requestData['unique_id'],function(){                     throw_error('token获取失败');                 }); 
                $service = new UgcService(json_decode($token),$config);
                $dates = $service->get_item_rates_by_item_id($requestData['item_id'],$requestData['start_time'],$requestData['end_time'],$requestData['offset']);
                foreach($dates as  $date) {
                    $data[] = [
                        'product_id' => $date->id,
                        'product_shopId' => $date->shopId,
                        'product_rating' => $date->rating,
                        'product_rateContent' => $date->rateContent,
                        'product_ratedAt' => $date->ratedAt,
                        'product_itemRates' => $date->itemRates,
                        'product_orderRateReply' => $date->orderRateReply,
                        'product_rateTag' => $date->rateTag,
                    ];
                }
                return $data;
                break;
            case "Mt":

                break;
            case "Tdd":
                break;
        }
    }

    /** 获取多个商品的评论
     * @param $item_ids 商品id
     * @param $start_time   开始时间,只能查询最近90天的数据
     * @param $end_time   结束时间
     * @param $offset 页面偏移量
     * @param $page_size 页面大小
     * @return mixed
     */
    public function getItemRatesByItemIds(Request $request)
    {
        $requestData = $request->only([
            'item_ids','start_time','end_time','offset','page_size','type','unique_id'
        ]);
        switch($requestData['type']){
            case "Elm":
                $config = new Config(env('PLATFORM_KEY'),env('PLATFORM_SECRET'), true);
                $token = Cache::get('token_'.$requestData['unique_id'],function(){                     throw_error('token获取失败');                 }); 
                $service = new UgcService(json_decode($token),$config);
                $dates = $service->get_item_rates_by_item_ids($requestData['item_ids'],$requestData['start_time'],$requestData['end_time'],$requestData['page_size'],$requestData['offset']);
                foreach($dates as  $date) {
                    $data[] = [
                        'product_id' => $date->id,
                        'product_shopId' => $date->shopId,
                        'product_rating' => $date->rating,
                        'product_rateContent' => $date->rateContent,
                        'product_ratedAt' => $date->ratedAt,
                        'product_itemRates' => $date->itemRates,
                        'product_orderRateReply' => $date->orderRateReply,
                        'product_rateTag' => $date->rateTag,
                    ];
                }
                return $data;
                break;
            case "Mt":

                break;
            case "Tdd":
                break;
        }
    }

    /** 获取多个商品未回复的评论
     * @param $item_ids 店铺id
     * @param $start_time   开始时间,只能查询最近90天的数据
     * @param $end_time   结束时间
     * @param $offset 页面偏移量
     * @param $page_size 页面大小
     * @return mixed
     */
    public function getUnreplyItemRatesByItemIds(Request $request)
    {
        $requestData = $request->only([
            'item_ids','start_time','end_time','offset','page_size','type','unique_id'
        ]);
        switch($requestData['type']){
            case "Elm":
                $config = new Config(env('PLATFORM_KEY'),env('PLATFORM_SECRET'), true);
                $token = Cache::get('token_'.$requestData['unique_id'],function(){                     throw_error('token获取失败');                 }); 
                $service = new UgcService(json_decode($token),$config);
                $dates = $service->get_unreply_item_rates_by_item_ids($requestData['item_ids'],$requestData['start_time'],$requestData['end_time'],$requestData['offset'],$requestData['page_size']);
                foreach($dates as  $date) {
                    $data[] = [
                        'product_id' => $date->id,
                        'product_shopId' => $date->shopId,
                        'product_rating' => $date->rating,
                        'product_rateContent' => $date->rateContent,
                        'product_ratedAt' => $date->ratedAt,
                        'product_itemRates' => $date->itemRates,
                        'product_orderRateReply' => $date->orderRateReply,
                        'product_rateTag' => $date->rateTag,
                    ];
                }
                return $data;
                break;
            case "Mt":

                break;
            case "Tdd":
                break;
        }
    }

    /** 回复指定类型的评论
     * @param $rate_id 评论编号
     * @param $reply_type 评论类型
     * @param $reply 回复的内容
     * @return mixed
     */
    public function replyRateByRateId(Request $request)
    {
        $requestData = $request->only([
            'rate_id','reply_type','reply','type','unique_id'
        ]);
        switch($requestData['type']){
            case "Elm":
                $config = new Config(env('PLATFORM_KEY'),env('PLATFORM_SECRET'), true);
                $token = Cache::get('token_'.$requestData['unique_id'],function(){                     throw_error('token获取失败');                 }); 
                $service = new UgcService(json_decode($token),$config);
                $dates = $service->reply_rate_by_rate_id($requestData['rate_id'],$requestData['reply_type'],$requestData['reply']);
                foreach($dates as  $date) {
                    $data[] = [
                        'product_id' => $date->id,
                        'product_shopId' => $date->shopId,
                        'product_rating' => $date->rating,
                        'product_rateContent' => $date->rateContent,
                        'product_ratedAt' => $date->ratedAt,
                        'product_itemRates' => $date->itemRates,
                        'product_orderRateReply' => $date->orderRateReply,
                        'product_rateTag' => $date->rateTag,
                    ];
                }
                return $data;
                break;
            case "Mt":

                break;
            case "Tdd":
                break;
        }
    }

    /** 回复指定类型的评论
     * @param $rate_ids  评论编号
     * @param $reply_type 评论类型
     * @param $reply 回复的内容
     * @return mixed
     */
    public function replyRateByRateIds(Request $request)
    {
        $requestData = $request->only([
            'rate_ids','reply_type','reply','type','unique_id'
        ]);
        switch($requestData['type']){
            case "Elm":
                $config = new Config(env('PLATFORM_KEY'),env('PLATFORM_SECRET'), true);
                $token = Cache::get('token_'.$requestData['unique_id'],function(){                     throw_error('token获取失败');                 }); 
                $service = new UgcService(json_decode($token),$config);
                $dates = $service->reply_rate_by_rate_ids($requestData['rate_ids'],$requestData['reply_type'],$requestData['reply']);
                foreach($dates as  $date) {
                    $data[] = [
                        'product_id' => $date->id,
                        'product_shopId' => $date->shopId,
                        'product_rating' => $date->rating,
                        'product_rateContent' => $date->rateContent,
                        'product_ratedAt' => $date->ratedAt,
                        'product_itemRates' => $date->itemRates,
                        'product_orderRateReply' => $date->orderRateReply,
                        'product_rateTag' => $date->rateTag,
                    ];
                }
                return $data;
                break;
            case "Mt":

                break;
            case "Tdd":
                break;
        }
    }

    /** 回复订单未回复的评论
     * @param $order_id 订单id
     * @param $reply 回复内容
     * @return mixed
     */
    public function replyRateByOrderId(Request $request)
    {
        $requestData = $request->only([
            'order_id','reply','type','unique_id'
        ]);
        switch($requestData['type']){
            case "Elm":
                $config = new Config(env('PLATFORM_KEY'),env('PLATFORM_SECRET'), true);
                $token = Cache::get('token_'.$requestData['unique_id'],function(){                     throw_error('token获取失败');                 }); 
                $service = new UgcService(json_decode($token),$config);
                $dates = $service->reply_rate_by_order_id($requestData['order_id'],$requestData['reply']);
                foreach($dates as  $date) {
                    $data[] = [
                        'product_id' => $date->id,
                        'product_shopId' => $date->shopId,
                        'product_rating' => $date->rating,
                        'product_rateContent' => $date->rateContent,
                        'product_ratedAt' => $date->ratedAt,
                        'product_itemRates' => $date->itemRates,
                        'product_orderRateReply' => $date->orderRateReply,
                        'product_rateTag' => $date->rateTag,
                    ];
                }
                return $data;
                break;
            case "Mt":

                break;
            case "Tdd":
                break;
        }
    }

    /** 批量回复订单未回复的评论       删除了一个reply参数，重复给了两个
     * @param $order_ids 订单id
     * @param $reply 回复信息
     * @param $reply 回复信息
     * @return mixed
     */
    public function replyCommentByOrderIds(Request $request)
    {
        $requestData = $request->only([
            'order_ids','reply','type','unique_id'
        ]);
        switch($requestData['type']){
            case "Elm":
                $config = new Config(env('PLATFORM_KEY'),env('PLATFORM_SECRET'), true);
                $token = Cache::get('token_'.$requestData['unique_id'],function(){                     throw_error('token获取失败');                 }); 
                $service = new UgcService(json_decode($token),$config);
                $dates = $service->reply_comment_by_order_ids($requestData['order_ids'],$requestData['reply']);
                foreach($dates as  $date) {
                    $data[] = [
                        'product_id' => $date->id,
                        'product_shopId' => $date->shopId,
                        'product_rating' => $date->rating,
                        'product_rateContent' => $date->rateContent,
                        'product_ratedAt' => $date->ratedAt,
                        'product_itemRates' => $date->itemRates,
                        'product_orderRateReply' => $date->orderRateReply,
                        'product_rateTag' => $date->rateTag,
                    ];
                }
                return $data;
                break;
            case "Mt":

                break;
            case "Tdd":
                break;
        }
    }

    /** 回复商品回复的评论
     * @param $item_id 商品id
     * @param $reply 回复内容
     * @param $start_time   开始时间,只能查询最近90天的数据
     * @param $end_time   结束时间
     * @return mixed
     */
    public function replyRatesByItemId(Request $request)
    {
        $requestData = $request->only([
            'item_id','reply','start_time','end_time','type','unique_id'
        ]);
        switch($requestData['type']){
            case "Elm":
                $config = new Config(env('PLATFORM_KEY'),env('PLATFORM_SECRET'), true);
                $token = Cache::get('token_'.$requestData['unique_id'],function(){                     throw_error('token获取失败');                 }); 
                $service = new UgcService(json_decode($token),$config);
                $dates = $service->reply_rates_by_item_id($requestData['item_id'],$requestData['reply'],$requestData['start_time'],$requestData['end_time']);
                foreach($dates as  $date) {
                    $data[] = [
                        'product_id' => $date->id,
                        'product_shopId' => $date->shopId,
                        'product_rating' => $date->rating,
                        'product_rateContent' => $date->rateContent,
                        'product_ratedAt' => $date->ratedAt,
                        'product_itemRates' => $date->itemRates,
                        'product_orderRateReply' => $date->orderRateReply,
                        'product_rateTag' => $date->rateTag,
                    ];
                }
                return $data;
                break;
            case "Mt":

                break;
            case "Tdd":
                break;
        }
    }

    /** 回复多个商品评论
     * @param $item_ids 商品d
     * @param $reply 回复信息
     * @param $start_time 开始时间,只能查询最近90天的数据
     * @param $end_time 结束时间
     * @return mixed
     */
    public function replyRatesByItemIds(Request $request)
    {
        $requestData = $request->only([
            'item_ids','reply','start_time','end_time','type','unique_id'
        ]);
        switch($requestData['type']){
            case "Elm":
                $config = new Config(env('PLATFORM_KEY'),env('PLATFORM_SECRET'), true);
                $token = Cache::get('token_'.$requestData['unique_id'],function(){                     throw_error('token获取失败');                 }); 
                $service = new UgcService(json_decode($token),$config);
                $dates = $service->reply_rates_by_item_ids($requestData['item_ids'],$requestData['reply'],$requestData['start_time'],$requestData['end_time']);
                foreach($dates as  $date) {
                    $data[] = [
                        'product_id' => $date->id,
                        'product_shopId' => $date->shopId,
                        'product_rating' => $date->rating,
                        'product_rateContent' => $date->rateContent,
                        'product_ratedAt' => $date->ratedAt,
                        'product_itemRates' => $date->itemRates,
                        'product_orderRateReply' => $date->orderRateReply,
                        'product_rateTag' => $date->rateTag,
                    ];
                }
                return $data;
                break;
            case "Mt":

                break;
            case "Tdd":
                break;
        }
    }
}
